var Zan = require('../../../dist/index.js');
let network = null;
let utils = null;
let api = null;
const app = getApp();
let start = 0;
Page(Object.assign({}, Zan.TopTips, Zan.Toast, {

  /**
   * 页面的初始数据
   */
  data: {
    txDialog:false,
    totalIncome: 0,
    value2:0,
    txlist: [],
    isSubmit: false,
    saleInfo: null,
    radiochecked:0,
    recordsTotal: 0,
    amount:'',
    nodata: {
      msg: '没有数据',
      notype: 2,
      imgurl: '../../../images/nodatas.png',
      nourl: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    
    this.onPullDownRefresh();
    this.setData({
      totalIncome: app.globalData.userInfo.totalIncome
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    start = 0;
    this.setData({
      txlist:[]
    })
    network._post_json(this, api.getSaleRule, {}, success);
    network._post_page_json(this, api.Presentrecord, start, {}, txlistsuccess);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // if (this.data.txlist.length < this.data.recordsTotal) {
    //   start = start + 20;
    //   network._post_page_json(this, api.Presentrecord, start, {}, txlistsuccess);
    // }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  // radioChange:function(e){
  //   var radiotxt = e.detail.value;
  //   if (radiotxt == "微信零钱"){
  //     this.setData({
  //       radiochecked:1
  //     })
  //   }else{
  //     this.setData({
  //       radiochecked: 0
  //     })
  //   }
  // },
  txclose: function (e) {
    this.setData({
      txDialog: !this.data.txDialog,
    })
  },
  inputamount:function(e){
    this.setData({
      amount: Math.floor(e.detail.value * 100) / 100
    })
  },

  //提交数据
  formSubmit: function (e) {
    var reg = new RegExp("^(([1-9][0-9]*\.[0-9][0-9]*)|([0]\.[0-9][0-9]*)|([1-9][0-9]*)|([0]{1}))$");
    console.log("====================================");
    console.log(this.data.amount);

    
    
    if (this.data.amount) {
      if (reg.test(this.data.amount)) {
        if (this.data.amount * 100 > this.data.totalIncome) {
          this.showZanTopTips(2, '提现金额超出收益总额');
        } else if (this.data.amount < this.data.value2 ) {
          this.showZanTopTips(2,(this.data.value2) + '元以上可提现');
        } else {
          network._post_json(this, api.applyWithdrawals, { amount: this.data.amount}, txsuccess);
        }
      } else {
        this.showZanTopTips(2, '提现金额不合法');
      }
    } else {
      console.log("请输入提现金额");
      this.showZanTopTips(2, '请输入提现金额');
      this.setData({
        value1:''
      })
    }


  }

}))

var success = function (that, res) {
  if (res.data.data) {
    that.setData({
      saleInfo: res.data.data,
      value2: parseInt(res.data.data.value2)
    })
  }
}

var txsuccess = function (that, res) {
  if(res.data.data){
    network._post_json(that, api.MakeMoney, { id: res.data.data.id }, txsqsuccess);
  }
}
var txsqsuccess = function(that,res){
  if (res.data.data.result_code == "SUCCESS") {
    that.showZanToast('申请成功');
  }else{
    that.showZanTopTips(2, '提现失败');
  }
}
var txlistsuccess = function(that,res){

  if (res.data.data != '没有提现记录'){

    that.setData({
      txlist:res.data.data,
      recordsTotal: res.data.data.length
    })
  }
}
